package com.why.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.commonresults.Result;
import com.why.statistics.client.UserClient;
import com.why.statistics.bean.Daily;
import com.why.statistics.mapper.DailyMapper;
import com.why.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-02-24
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private UserClient userClient;

    @Override
    public void createStatisticsByDay(String day) {
        //删除已存在的统计对象
        QueryWrapper<Daily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculated", day);
        baseMapper.delete(dayQueryWrapper);
        //远程调用获取统计信息
        Result registerCount = userClient.registerCount(day);
        Integer register = (Integer) registerCount.getData().get("countRegister");
        //获取的数据加入到数据库


        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        //创建统计对象
        Daily daily = new Daily();

        daily.setRegisterNum(register);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);
        baseMapper.insert(daily);
    }

    /**
     * 图表显示接口
     * 将数据和日期分开返回
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> showDateByType(String type, String begin, String end) {
        QueryWrapper<Daily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculated");
        dayQueryWrapper.between("date_calculated", begin, end);
        //查询出所有的数据
        List<Daily> dayList = baseMapper.selectList(dayQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        //数据集合
        List<Integer> dataList = new ArrayList<Integer>();
        //日期集合
        List<String> dateList = new ArrayList<String>();
        //遍历数据集合，将日期和数据分开保存
        for (int i = 0; i < dayList.size(); i++) {
            Daily daily = dayList.get(i);
            //保存日期
            dateList.add(daily.getDateCalculated());
            //保存数据
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        map.put("dataList", dataList);
        map.put("dateList", dateList);
        System.out.println("日期数据为: " + dateList);
        System.out.println("数据为" + dataList);
        return map;
    }
}
